module AdminUtil
  module Model
    def self.included(base)
      base.extend ClassMethods
    end
    
    module ClassMethods
      attr_accessor :current_user_proc
      attr_accessor :session_uniq_key_proc
      attr_accessor :mobile_user_proc
      def current_user
        pp '11111111111111111111'
        ActiveRecord::Base.current_user_proc.call if ActiveRecord::Base.current_user_proc
      end
      
      def session_uniq_key
        ActiveRecord::Base.session_uniq_key_proc.call if ActiveRecord::Base.session_uniq_key_proc
      end
      
      def mobile_user
        ActiveRecord::Base.mobile_user_proc.call if ActiveRecord::Base.mobile_user_proc
      end
    end
    
    def current_user
      self.class.current_user
    end
    
    def session_uniq_key
      self.class.session_uniq_key
    end
    
    def mobile_user
      self.class.mobile_user
    end
  end
  
  module Controller
    def self.included(base)
      base.prepend_before_filter :assign_current_user_to_models
    end
    
    def assign_current_user_to_models
      ActiveRecord::Base.current_user_proc = proc{send(:get_logged_admin)}
      ActiveRecord::Base.session_uniq_key_proc = proc{send(:get_session_uniq_key)}
      ActiveRecord::Base.mobile_user_proc = proc{send(:get_mobile_user)}
    end
  end
  
  def get_logged_admin
    pp '+++++++++++++++++++++++++++++++++'
    Admin.find_by_id(session[:admin])
  end
  
  def get_session_uniq_key
    session[:uniq_key]
  end
  
  def get_mobile_admin
    session[:admin]
  end
  
end